#!/bin/sh

# run from directory where this script is
cd `dirname $0`
EXAMPLE_DIR=`pwd`

# check whether echo has the -e option
if test "`echo -e`" = "-e" ; then ECHO=echo ; else ECHO="echo -e" ; fi

$ECHO
$ECHO "$EXAMPLE_DIR : starting"
$ECHO
$ECHO "This example shows how to use PostProc codes to calculate the total"
$ECHO "and projected DOS, and to plot the Fermi Surface using XCrysDen."

# set the needed environment variables
. ../../../environment_variables

# required executables and pseudopotentials
BIN_LIST="pw.x dos.x projwfc.x fs.x"
PSEUDO_LIST="Ni.pz-nd-rrkjus.UPF"

$ECHO
$ECHO "  executables directory: $BIN_DIR"
$ECHO "  pseudo directory:      $PSEUDO_DIR"
$ECHO "  temporary directory:   $TMP_DIR"
$ECHO "  checking that needed directories and files exist...\c"

# check for directories
for DIR in "$BIN_DIR" "$PSEUDO_DIR" ; do
    if test ! -d $DIR ; then
        $ECHO
        $ECHO "ERROR: $DIR not existent or not a directory"
        $ECHO "Aborting"
        exit 1
    fi
done
for DIR in "$TMP_DIR" "$EXAMPLE_DIR/results" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/results

# check for executables
for FILE in $BIN_LIST ; do
    if test ! -x $BIN_DIR/$FILE ; then
        $ECHO
        $ECHO "ERROR: $BIN_DIR/$FILE not existent or not executable"
        $ECHO "Aborting"
        exit 1
    fi
done

# check for gnuplot
GP_COMMAND=`which gnuplot 2>/dev/null`
if [ "$GP_COMMAND" = "" ]; then
        $ECHO
        $ECHO "gnuplot not in PATH"
        $ECHO "Results will not be plotted"
fi

# check for pseudopotentials
for FILE in $PSEUDO_LIST ; do
    if test ! -r $PSEUDO_DIR/$FILE ; then
       $ECHO
       $ECHO "Downloading $FILE to $PSEUDO_DIR...\c"
            $WGET $PSEUDO_DIR/$FILE $NETWORK_PSEUDO/$FILE 2> /dev/null
    fi
    if test $? != 0; then
        $ECHO
        $ECHO "ERROR: $PSEUDO_DIR/$FILE not existent or not readable"
        $ECHO "Aborting"
        exit 1
    fi
done
$ECHO " done"

# how to run executables
PW_COMMAND="$PARA_PREFIX $BIN_DIR/pw.x $PARA_POSTFIX"
DOS_COMMAND="$PARA_PREFIX $BIN_DIR/dos.x $PARA_POSTFIX"
PROJWFC_COMMAND="$PARA_PREFIX $BIN_DIR/projwfc.x $PARA_POSTFIX"
FS_COMMAND="$BIN_DIR/fs.x "
$ECHO
$ECHO "  running pw.x as:      $PW_COMMAND"
$ECHO "  running dos.x as:     $DOS_COMMAND"
$ECHO "  running projwfc.x as: $PROJWFC_COMMAND"
$ECHO "  running gnuplot as:   $GP_COMMAND"
$ECHO "  running fs.x as:      $FS_COMMAND"
$ECHO

# self-consistent calculation
cat > ni.scf.in << EOF
 &control
    calculation='scf'
    restart_mode='from_scratch',
    prefix='ni',
    pseudo_dir = '$PSEUDO_DIR/',
    outdir='$TMP_DIR/'
 /
 &system
    ibrav=2, celldm(1) =6.48, nat=1, ntyp=1,
    nspin = 2,  starting_magnetization(1)=0.7,
    ecutwfc = 24.0, ecutrho = 288.0,
    occupations='smearing', smearing='mv', degauss=0.02
 /
 &electrons
    conv_thr = 1.0e-10
    mixing_beta = 0.7
 /
ATOMIC_SPECIES
 Ni 58.69 Ni.pz-nd-rrkjus.UPF
ATOMIC_POSITIONS alat
 Ni 0.0 0.0 0.0
K_POINTS
  60
   0.0625000  0.0625000  0.0625000   1.00
   0.0625000  0.0625000  0.1875000   3.00
   0.0625000  0.0625000  0.3125000   3.00
   0.0625000  0.0625000  0.4375000   3.00
   0.0625000  0.0625000  0.5625000   3.00
   0.0625000  0.0625000  0.6875000   3.00
   0.0625000  0.0625000  0.8125000   3.00
   0.0625000  0.0625000  0.9375000   3.00
   0.0625000  0.1875000  0.1875000   3.00
   0.0625000  0.1875000  0.3125000   6.00
   0.0625000  0.1875000  0.4375000   6.00
   0.0625000  0.1875000  0.5625000   6.00
   0.0625000  0.1875000  0.6875000   6.00
   0.0625000  0.1875000  0.8125000   6.00
   0.0625000  0.1875000  0.9375000   6.00
   0.0625000  0.3125000  0.3125000   3.00
   0.0625000  0.3125000  0.4375000   6.00
   0.0625000  0.3125000  0.5625000   6.00
   0.0625000  0.3125000  0.6875000   6.00
   0.0625000  0.3125000  0.8125000   6.00
   0.0625000  0.3125000  0.9375000   6.00
   0.0625000  0.4375000  0.4375000   3.00
   0.0625000  0.4375000  0.5625000   6.00
   0.0625000  0.4375000  0.6875000   6.00
   0.0625000  0.4375000  0.8125000   6.00
   0.0625000  0.4375000  0.9375000   6.00
   0.0625000  0.5625000  0.5625000   3.00
   0.0625000  0.5625000  0.6875000   6.00
   0.0625000  0.5625000  0.8125000   6.00
   0.0625000  0.6875000  0.6875000   3.00
   0.0625000  0.6875000  0.8125000   6.00
   0.0625000  0.8125000  0.8125000   3.00
   0.1875000  0.1875000  0.1875000   1.00
   0.1875000  0.1875000  0.3125000   3.00
   0.1875000  0.1875000  0.4375000   3.00
   0.1875000  0.1875000  0.5625000   3.00
   0.1875000  0.1875000  0.6875000   3.00
   0.1875000  0.1875000  0.8125000   3.00
   0.1875000  0.3125000  0.3125000   3.00
   0.1875000  0.3125000  0.4375000   6.00
   0.1875000  0.3125000  0.5625000   6.00
   0.1875000  0.3125000  0.6875000   6.00
   0.1875000  0.3125000  0.8125000   6.00
   0.1875000  0.4375000  0.4375000   3.00
   0.1875000  0.4375000  0.5625000   6.00
   0.1875000  0.4375000  0.6875000   6.00
   0.1875000  0.4375000  0.8125000   6.00
   0.1875000  0.5625000  0.5625000   3.00
   0.1875000  0.5625000  0.6875000   6.00
   0.1875000  0.6875000  0.6875000   3.00
   0.3125000  0.3125000  0.3125000   1.00
   0.3125000  0.3125000  0.4375000   3.00
   0.3125000  0.3125000  0.5625000   3.00
   0.3125000  0.3125000  0.6875000   3.00
   0.3125000  0.4375000  0.4375000   3.00
   0.3125000  0.4375000  0.5625000   6.00
   0.3125000  0.4375000  0.6875000   6.00
   0.3125000  0.5625000  0.5625000   3.00
   0.4375000  0.4375000  0.4375000   1.00
   0.4375000  0.4375000  0.5625000   3.00
EOF
$ECHO "  running the scf calculation for Ni...\c"
$PW_COMMAND < ni.scf.in > ni.scf.out
check_failure $?
$ECHO " done"

# DOS calculation for Ni
cat > ni.dos.in << EOF
 &control
    calculation='nscf'
    prefix='ni',
    pseudo_dir = '$PSEUDO_DIR/',
    outdir='$TMP_DIR/'
 /
 &system
    ibrav=2, celldm(1) =6.48, nat=1, ntyp=1,
    nspin = 2,  starting_magnetization(1)=0.7,
    ecutwfc = 24.0, ecutrho = 288.0, nbnd=8,
    occupations='tetrahedra'
 /
 &electrons
    conv_thr = 1.0e-10
    mixing_beta = 0.7
 /
ATOMIC_SPECIES
 Ni 58.69 Ni.pz-nd-rrkjus.UPF
ATOMIC_POSITIONS alat
 Ni 0.0 0.0 0.0
K_POINTS {automatic}
 12 12 12 0 0 0
EOF

cat > ni.dos2.in << EOF
 &dos
    outdir='$TMP_DIR/'
    prefix='ni'
    fildos='ni.dos',
    Emin=5.0, Emax=25.0, DeltaE=0.1
 /
EOF

$ECHO "  running DOS calculation for Ni...\c"
$PW_COMMAND < ni.dos.in > ni.dos.out
check_failure $?
$DOS_COMMAND < ni.dos2.in > ni.dos2.out
check_failure $?
$ECHO " done"

cat > ni.pdos.in << EOF
 &projwfc
    outdir='$TMP_DIR/'
    prefix='ni'
    Emin=5.0, Emax=25.0, DeltaE=0.1
    ngauss=1, degauss=0.02
 /
EOF
$ECHO "  running PDOS calculation for Ni...\c"
$PROJWFC_COMMAND < ni.pdos.in > ni.pdos.out
check_failure $?
$ECHO " done"

$ECHO
$ECHO "  Fermi Surface plot Spin-Polarized case..."

# self-consistent calculation - Spin-Polarized (SP) case

cat > ni.scf_SP.in << EOF
 &control
    calculation='scf'
    restart_mode='from_scratch',
    prefix='ni',
    pseudo_dir = '$PSEUDO_DIR/',
    outdir='$TMP_DIR/'
 /
 &system
    ibrav=2, celldm(1) =6.48, nat=1, ntyp=1,
    ecutwfc = 24.0, ecutrho = 288.0,
    occupations='smearing',
    smearing='mv',
    degauss=0.02
    nspin=2,
    starting_magnetization(1)=0.8
 /
 &electrons
    conv_thr = 1.0e-10
    mixing_beta = 0.7
 /
ATOMIC_SPECIES
 Ni 58.69 Ni.pz-nd-rrkjus.UPF
ATOMIC_POSITIONS alat
 Ni 0.0 0.0 0.0
K_POINTS {automatic}
 8 8 8 0 0 0
EOF
$ECHO "  running the scf calculation  spin-polarization ... \c"
$PW_COMMAND < ni.scf_SP.in > ni.scf0.SP.out
check_failure $?
$ECHO " done"

cat > ni.fs_SP.in << EOF
 &control
    calculation='nscf'
    prefix='ni',
    pseudo_dir = '$PSEUDO_DIR/',
    outdir='$TMP_DIR/'
 /
 &system
    ibrav=2,
    celldm(1) =6.48,
    nat=1,
    ntyp=1,
    ecutwfc = 24.0,
    ecutrho = 288.0,
    nbnd=8
    nspin=2,
    starting_magnetization(1)=0.8
    occupations='tetrahedra'
 /
 &electrons
    conv_thr = 1.0e-10
    mixing_beta = 0.7
 /
ATOMIC_SPECIES
 Ni 58.69 Ni.pz-nd-rrkjus.UPF
ATOMIC_POSITIONS alat
 Ni 0.0 0.0 0.0
K_POINTS {automatic}
16 16 16 0 0 0
EOF

$ECHO "  running the Fermi Surface calculation ... \c"
$PW_COMMAND   < ni.fs_SP.in > ni.fs_SP.out
check_failure $?
$ECHO " done"

cat > FS.in <<EOF
&fermi
    outdir='$TMP_DIR/'
    prefix='ni'
/
EOF
$FS_COMMAND < FS.in > FS.out
check_failure $?

$ECHO
$ECHO "  Use 'xcrysden --bxsf results/ni_fsXX.bxsf', XX=up,dw to plot the Fermi Surface\c"
$ECHO " done"

##################

$ECHO
$ECHO "  Fermi Surface plot Non-Spin-Polarized (NSP) case..."

# self-consistent calculation - Non-Spin-Polarised case

cat > ni.scf_NSP.in << EOF
 &control
    calculation='scf'
    restart_mode='from_scratch',
    prefix='ni',
    pseudo_dir = '$PSEUDO_DIR/',
    outdir='$TMP_DIR/'
 /
 &system
    ibrav=2, celldm(1) =6.48, nat=1, ntyp=1,
    ecutwfc = 24.0, ecutrho = 288.0,
    occupations='smearing',
    smearing='mv',
    degauss=0.02
 /
 &electrons
    conv_thr = 1.0e-10
    mixing_beta = 0.7
 /
ATOMIC_SPECIES
 Ni 58.69 Ni.pz-nd-rrkjus.UPF
ATOMIC_POSITIONS alat
 Ni 0.0 0.0 0.0
K_POINTS {automatic}
 8 8 8 0 0 0
EOF
$ECHO "  running the scf calculation  non spin-polarized case ... \c"
$PW_COMMAND < ni.scf_NSP.in > ni.scf0.NSP.out
check_failure $?
$ECHO " done"

cat > ni.fs_NSP.in << EOF
 &control
    calculation='nscf'
    prefix='ni',
    pseudo_dir = '$PSEUDO_DIR/',
    outdir='$TMP_DIR/'
 /
 &system
    ibrav=2,
    celldm(1) =6.48,
    nat=1,
    ntyp=1,
    ecutwfc = 24.0,
    ecutrho = 288.0,
    nbnd=8
    occupations='tetrahedra'
 /
 &electrons
    conv_thr = 1.0e-10
    mixing_beta = 0.7
 /
ATOMIC_SPECIES
 Ni 58.69 Ni.pz-nd-rrkjus.UPF
ATOMIC_POSITIONS alat
 Ni 0.0 0.0 0.0
K_POINTS automatic
16 16 16 0 0 0
EOF

$ECHO "  running the Fermi Surface calculation ... \c"
$PW_COMMAND   < ni.fs_NSP.in > ni.fs_NSP.out
check_failure $?
$ECHO " done"

cat > FS.in <<EOF
&fermi
    outdir='$TMP_DIR/'
    prefix='ni'
/
EOF
$FS_COMMAND < FS.in > FS.out
check_failure $?


$ECHO
$ECHO "  Use 'xcrysden --bxsf results/ni_fs.bxsf' to plot the Fermi Surface\c"
$ECHO " done"

$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/ni.*

$ECHO
$ECHO "$EXAMPLE_DIR: done"
